<?php
/*********************/
/*                   */
/*  前台购物车控制     */
/*   BY voiet        */
/*   2009-11-21       */
/*                   */
/*********************/

class cct_cart extends ctl_cart
{

	var $noCache = true;
	
	function index( )
	{
			$this->title = "查看购物车";
			$sale = $this->system->loadmodel( "trading/sale" );
			$trading = $sale->getcartobject( $this->cart, $GLOBALS['runtime']['member_lv'], true );
			$number = count( $trading['products'] ) + count( $trading['gift_e'] ) + count( $trading['package'] );
			if ( $number != $_COOKIE['CART_COUNT'] )
			{
					$this->system->setcookie( "CART_COUNT", $number );
			}
			$this->pagedata['trading'] =& $trading;
			$acart =$this->objCart->getcartweight();
			$this->pagedata['cartcn'] = $acart['cn'];
			$this->pagedata['cartkr'] = $acart['kr'];
			$cur = $this->system->loadmodel( "system/cur" );
			$aCur = $cur->getformat( $this->system->request['cur'] );
			$this->pagedata['currency'] = json_encode( $aCur );
			header( "Expires: -1" );
			header( "Pragma: no-cache" );
			header( "Cache-Control: no-cache, no-store" );
			$this->output( );
	}
	
	function shipping( )
	{
			$sale = $this->system->loadmodel( "trading/sale" );
			$trading = $sale->getcartobject( $this->cart, $GLOBALS['runtime']['member_lv'], true );
			$myacart =$this->objCart->getcartweight();
			$shipping = $this->system->loadmodel( "trading/delivery" );
			$aShippings = $shipping->getdltypebyarea( $_POST['area'] );
			foreach ( $aShippings as $k => $s )
			{
					
					//$aShippings[$k]['price'] = cal_fee( $s['expressions'], $trading['weight'], $trading['pmt_b']['totalPrice'], $s['price'] );
					//2009-11-26 voiet 更新运费
					if($s['dt_id']>=1 && $s['dt_id']<=14)
						$price_cn[$s['dt_id']]=cal_fee( $s['expressions'], $myacart['cn']['weight'], $trading['pmt_b']['totalPrice'], $s['price'] );
					elseif($s['dt_id']>=15 && $s['dt_id']<=18)
						$price_kr[$s['dt_id']]=cal_fee( $s['expressions'], $myacart['kr']['weight'], $trading['pmt_b']['totalPrice'], $s['price'] );
						
					if($myacart['cn']['weight']>0 && $myacart['kr']['weight']==0)$aShippings[$k]['price']=$price_cn[$s['dt_id']];
					elseif($myacart['kr']['weight']>0 && $myacart['cn']['weight']==0)$aShippings[$k]['price']=$price_kr[$s['dt_id']];
					else {
						if($s['dt_id']==15)$aShippings[$k]['price']=$price_kr[15] + $price_cn[3];
						elseif($s['dt_id']==16)$aShippings[$k]['price']=$price_kr[16] + $price_cn[2];
						elseif($s['dt_id']==17)$aShippings[$k]['price']=$price_kr[17] + $price_cn[1];
						elseif($s['dt_id']==18)$aShippings[$k]['price']=$price_kr[18] + $price_cn[13];
					
					}
					if($s['dt_id'] == 13){
							if($this->member['member_id'] == 2161 || $this->member['member_id'] == 2204 || $this->member['member_id'] == 6166 )
								$aShippings[$k]['price'] = 0;
							elseif($this->member['member_id']==6318)$aShippings[$k]['price'] = 10;
					}
					
					$s['pad'] == 0 ? ( $aShippings[$k]['has_cod'] = 0 ) : ( $aShippings[$k]['has_cod'] = 1 );
					if ( $s['protect'] == 1 )
					{
								$aShippings[$k]['protect'] = true;
					}
					else
					{
								$aShippings[$k]['protect'] = false;
					}
			}
			$this->pagedata['shippings_cn'] = $myacart['cn']['weight'];
			$this->pagedata['shippings_kr'] = $myacart['kr']['weight'];
			$this->pagedata['shippings'] = $aShippings;
			$this->__tmpl = "cart/checkout_shipping.html";
			$this->output( );
	}
					
	function applycoupon( )
	{
		$this->begin( $this->system->mkurl( "cart", "index" ), null, E_ERROR | E_USER_ERROR | E_USER_WARNING );
		$oCoupon = $this->system->loadmodel( "trading/coupon" );
		if ( !empty( $_POST['coupon'] ) )
		{
			if($_POST['weight_kr'] > 0){
			
				trigger_error( "对不起，韩国区商品暂不支持优惠券的使用", E_USER_ERROR );
			}
			$oSale = $this->system->loadmodel( "trading/sale" );
			$oPromotion = $this->system->loadmodel( "trading/promotion" );
			$trading = $oSale->getcartobject( $this->cart, $GLOBALS['runtime']['member_lv'], true );
			if ( $trading['ifCoupon'] )
			{
				$member_id = $oCoupon->getcouponuid($_POST['coupon']);
				if($this->member['member_id'] == $member_id ){
					$oPromotion->apply_coupon_pmt( $trading, $_POST['coupon'], $GLOBALS['runtime']['member_lv'] );
				}
				else {
				
					trigger_error( "非本人优惠券或没有登陆", E_USER_ERROR );
				}
			}
			else
			{
				trigger_error( "有促销活动期间不允许使用优惠券", E_USER_ERROR );
			}
		}
		else
		{
			trigger_error( "请输入优惠券", E_USER_ERROR );
		}
		$this->end( true, "成功加入购物车", $this->system->mkurl( "cart", "index" ) );
	}
}

?>